<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace ApiModule;

use Nette\Object;
use Albireo\OAuthServer\OAuthServer;
use Nette\Web\User;

/**
 * OAuth Access
 *
 * @author Albireo Solutions
 */
class OAuthAccess extends Object {

    private $oauth;

    public function __construct() {
        $this->oauth = new OAuthServer();
    }

    public function generateAccess() {
        $this->oauth->checkRequest();
        return $this->oauth->generateAccessToken();
    }

    public function generateRequest() {
        $this->oauth->setRequestTokenQuery();
        $this->oauth->checkRequest();
        return $this->oauth->generateRequestToken();
    }

    public function checkAccess(\Nette\Web\User $user = null) {
        if (!$user->isLoggedIn()) {
            return false;
        }
        $this->oauth->checkRequest();
        return $this->oauth->getUser() == $user->getIdentity()->id;
    }
}

